using System.Web.Mvc;
using Animoso.Domain;
using Animoso.Domain.Data;
using System.Linq;

namespace Animoso.Web.Controllers
{
    public class EventController : ApplicationController
    {
        private readonly IRepository<Event> _eventRepository;
        private readonly ILabelRepository _labelRepository;

        public EventController(IRepository<Event> repository, ILabelRepository labelRepository)
        {
            _eventRepository = repository;
            _labelRepository = labelRepository;
        }

        public ActionResult Index()
        {
            string agendaTitle = _labelRepository.GetTextByLabel("agendaTitle");
            string agendaDescription = _labelRepository.GetTextByLabel("agendaDescription");

            ViewData["agendaTitle"] = agendaTitle;
            ViewData["agendaDescription"] = agendaDescription;
            
            return View(_eventRepository.GetAll());
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")] Event @event)
        {
            if (!ModelState.IsValid)
                return View();

            _eventRepository.Add(@event);
            return RedirectToAction("Index", "Event");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
            var @event = _eventRepository.GetById(id);
            return View(@event);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Event @event)
        {
            if (!ModelState.IsValid)
                return View(@event);

            _eventRepository.Save(@event);
            return RedirectToAction("Index", "Event");
        }
    }
}